package com.example.letgo.story;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.letgo.R;
import com.example.letgo.model.User;
import com.example.letgo.tool.CircleTransform;
import com.example.letgo.trip.TripDetailsActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


/**
 * Created by cc on 2018/2/11.
 */

public class StoryDetailsActivity extends AppCompatActivity{

    private WebView webView;
    private TextView username,storytime,text1,text2,text3,storyname;
    private ImageView img1,img2,head,story_back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stroy_details);
        webView = (WebView) findViewById(R.id.webview);
        story_back = findViewById(R.id.story_back);
        webView.setWebViewClient(new Client());
        webView.loadUrl("file:///android_asset/comment.html");

        //设置WebView支持JavaScript
        webView.getSettings().setJavaScriptEnabled(true);

        head = findViewById(R.id.story_user_head);
        storyname = findViewById(R.id.story_name);
        username = findViewById(R.id.story_user_name);
        storytime = findViewById(R.id.story_publish_time);
        text1 = findViewById(R.id.story_content_text1);
        text2 = findViewById(R.id.story_content_text2);
        text3 = findViewById(R.id.story_introduce_text3);
        img1 = findViewById(R.id.story_title_img);
        img2 = findViewById(R.id.story_content_img);

        //  接收StoryFragment传过来的值
        Bundle bundle = getIntent().getExtras();
        String storyOverPlace = bundle.getString("storyOverPlace");
        int storyUserId = bundle.getInt("storyUserId");
        String storyTime = bundle.getString("storyTime");
        String storyImg1 = bundle.getString("storyImg1");
        String storyImg2 = bundle.getString("storyImg2");
        String storyText1 = bundle.getString("storyText1");
        String storyText2 = bundle.getString("storyText2");
        String storyText3 = bundle.getString("storyText3");

        storyname.setText(storyOverPlace);
        storytime.setText(storyTime);
        text1.setText(storyText1);
        text2.setText(storyText2);
        text3.setText(storyText3);
        Picasso.with(this).load(storyImg1).into(img1);
        Picasso.with(this).load(storyImg2).into(img2);

        story_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        userinit(storyUserId);
    }

    private void userinit(final int storyUserId){

        OkGo.get("http://119.29.113.114:8080/gogogo/user")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("s",s);
                        Gson gson = new Gson();
                        Type listType = new TypeToken<LinkedList<User>>(){}.getType();
                        LinkedList<User> list = gson.fromJson(s, listType);
                        Map<String, Object> map=new HashMap<String, Object>();

                        for (int i = 0;i<list.size();i++){

                            if (storyUserId==list.get(i).getUserId()){
                                Log.i("TAG","................ "+list.get(i).getUserId());

                                String ImageUrl = "http://119.29.113.114:8080/gogogo/"+list.get(i).getUserImg();
                                String nickname = list.get(i).getUserNickName();

                                Picasso.with(StoryDetailsActivity.this).load(ImageUrl).transform(new CircleTransform()).into(head);
                                username.setText(nickname);
                            }
                        }
                    }
                });
    }

    class Client extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
